<template>
  <BaseContainer class="report-table-wrapper" v-loading="loading" element-loading-text="加载中..."
    element-loading-background="rgba(255, 255, 255, 0.8)">
    <div class="r-header">
      <p>养老机构连锁运营补贴申请表</p>
    </div>
    <div class="flex gap-20">
      <div style="min-width: 140px">填报单位（盖章）：{{ modelData.organizationName }}</div>
      <div>填报时间：{{ modelData.fillingTime }}</div>
    </div>
    <ReportTable :column="colConfigList" :isDetail="isDetail" :moreNewsModel="moreNewsModel" :baseModel="baseModel"
      :model="modelData" @change="handleSave">
      <template v-slot:capitalLetters>
        <span>大写：</span>
        <span>{{ subsidy_big_money }}</span>
      </template>
      <template v-slot:note>
        <div class="w-100 tr flex-column">
          <p class="fc">本机构承诺以上及所附数据资料真实有效，如有不实，愿承担相关处罚。</p>
          <div class="p-r-50">
            <p>承办人签名： （单位盖章）</p>
            <p style="padding-right: 3vw">年 月 日</p>
          </div>
        </div>
      </template>
      <template v-slot:opinion>
        <div class="w-100" style="line-height: 30px">
          <p style="padding-right: 2vw; text-align: right; letter-spacing: 4px">区民政局：（章）</p>
          <p style="padding-right: 3vw; text-align: right; letter-spacing: 10px">年 月 日</p>
        </div>
      </template>
    </ReportTable>
    <OrgArchives ref="orgArchivesEl"></OrgArchives>
  </BaseContainer>
</template>

<script setup lang="ts">
import OrgArchives from '../../OrgArchives/index.vue'
import ReportTable from '../../ReportTable/index.vue'

import apiMod from '@/apiMod'
import { useCommonStore } from '@/stores/common'
import { dealBigMoney, filterEmptyObj } from '@/utils/util'
const { setTempParams, setGloablParams, setMoreSearch } = useCommonStore()

const props = withDefaults(
  defineProps<{
    creditCode?: string | number
    params?: any
    editData?: any
    isDetail?: boolean
    caseId?: string | number
    custome?: boolean
    approvalStatus?: string | number
  }>(),
  {
    editData: {},
  }
)

const emit = defineEmits(['save'])
const orgArchivesEl = ref(null)
const loading = ref(false)
const model = ref({
  year: '',
  month: '',
})
const baseModel: any = shallowRef({})
let moreNewsModel: any = ref({
})
let modelData: any = ref({
})

const colConfigList = ref([])

const subsidy_big_money = computed(() => {
  const money = modelData.value.subsidyMoney
  if (money && !isNaN(Number(money))) {
    return dealBigMoney(+money)
  } else {
    return ''
  }
})
const onClose = () => {
  setTempParams({})
  setGloablParams({})
  setMoreSearch({})
}

const tableColumnConfig = (data?: any) => {
  const emptyCol = Array.from(new Array(3))

  const chainList = emptyCol.map((_, index) => {
    let str = index ==0?'':index
    return [
      {
        m: '',
        prop: `chainOrganizationName${str}`,
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '',
        prop: `operationStartingTime${str}`,
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '',
        prop: `ratingLevel${str}`,
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '',
        prop: `levelStartingTime${str}`,
        mc: {
          rs: 1,
          cs: 1,
        },
      },
    ]
  })
  console.log(chainList,'chainListchainList')
  const config = [
    [
      {
        m: '养老机构运营单位基本情况',
        bl: 1,
        fs: 16,
        mc: {
          rs: 1,
          cs: 4,
        },
      },
    ],
    [
      { m: '单位名称', style: { width: '200px' } },
      {
        m: '',
        prop: 'organizationName',
        archives: true,
				setColor: true,
				archivesText: '档案',
				showLink: (col, model) => {
					orgArchivesEl.value?.getServiceOrgInfo(model.creditCode)
				},
      },
      { m: '法人代表', style: { width: '200px' } },
      { m: '', prop: 'legalUserName', setColor: true },
    ],
    [{ m: '单位地址' }, { m: '', setColor: true, prop: 'organizationAddress' }, { m: '邮政编码' }, { m: '', setColor: true, prop: 'postalCode' }],
    [{ m: '联系人' }, { m: '', setColor: true, prop: 'contactName' }, { m: '联系电话' }, { m: '', setColor: true, prop: 'contactPhone' }],
    [{ m: '开户银行' }, { setColor: true, m: '', prop: 'bankName' }, { m: '银行账号' }, { setColor: true, m: '', prop: 'bankAccount' }],
    [
      {
        m: '连锁基本信息',
        bl: 1,
        fs: 16,
        style: { borderTop: '0px solid #fff' },
        mc: {
          rs: 1,
          cs: 4,
        },
      },
    ],
    [
      {
        m: '连锁机构名称',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '运营起始时间',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '评定等级',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '等级起始时间',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
    ],
    ...chainList,
    [
      {
        m: '申请基本情况',
        bl: 1,
        fs: 16,
        style: { borderTop: '0px solid #fff' },
        mc: {
          rs: 1,
          cs: 4,
        },
      },
    ],
    [
      {
        m: '申报连锁机构数',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '',
        prop: 'chainOrganizationNum',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '补贴标准',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '',
        prop: 'subsidyStandards',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
    ],
    [
      {
        m: '补贴金额',
        align: 'center',
        mc: {
          rs: 1,
          cs: 1,
        },
      },
      {
        m: '',
        prop: 'subsidyMoney',
        align: 'left',
        mc: {
          rs: 1,
          cs: 2,
        },
      },
      {
        propName: '大写',
        prop: 'capitalLetters',
        disabled: true,
        align: 'left',
        mc: {
          rs: 1,
          cs: 2,
        },
      },
    ],
    [
      {
        prop: 'note',
        mc: {
          rs: 1,
          cs: 9,
        },
      },
    ],
    [
      {
        m: '区民政部门意见',
        mc: {
          rs: 1,
          cs: 2,
        },
      },
      {
        m: '',
        prop: 'opinion',
        mc: {
          rs: 1,
          cs: 7,
        },
      },
    ],
  ]

  return config
}
const getInitData = async () => {
  const params = props.params || {}
  const query = {
    year: '',
    month: '',
    creditCode: props.creditCode,
    projectId: props.params.subsidyProjectId,
  }
  if (params.belongDate) {
    query.year = params.belongDate.replace('年', '-').replace('月', '').split('-')[0]
    query.month = params.belongDate.replace('年', '-').replace('月', '').split('-')[1]
    model.value.year = query.year
    model.value.month = query.month
  } else {
    return
  }
  modelData.value = props.editData
  try {
    const condition = (props.caseId && props.isDetail) || props.custome || props.approvalStatus == -2
    loading.value = true
    let resultData = {}
    if (condition) {
      resultData = props.editData
    } else {
      const uptoData = await apiMod.view_metadata_statistics_chain_ls(query).then((res) => res.data)
      resultData = uptoData
    }
    baseModel.value = JSON.parse(JSON.stringify(resultData))
    const reportData = filterEmptyObj(resultData)

    let mixData = {
      ...props.editData,
      ...reportData,
    }

    loading.value = false

    modelData.value = mixData
    if (!condition) handleSave()
    colConfigList.value = tableColumnConfig()
  } catch (error) {
    loading.value = false
  }
}

watch(
  () => props.editData?.id,
  (val) => {
    if (val) {
      if (props.params) getInitData()
    }
  },
  {
    immediate: true,
  }
)

onMounted(() => {
  nextTick(() => {
    if (props.params) getInitData()
  })
})
const handleSave = () => {
  modelData.value.capitalLetters = subsidy_big_money.value
  emit('save', modelData.value, 'report_1008', props.editData)
}
</script>

<style scoped lang="scss">
.flex-column {
  display: flex;
  flex-flow: column;
  justify-content: space-around;
  height: 100px;
}

.text-underline {
  text-decoration: underline;
  padding: 0 6px;
}

.report-table-wrapper {
  width: 100%;
  margin: 0 auto;
  padding: 10px 30px;
  top: 0;

  .r-header {
    text-align: center;
    font-size: 18px;
    font-weight: bold;
    padding: 15px 0;

    .text-sm {
      font-weight: 400;
      font-size: 16px;
    }
  }
}
</style>
